package com.hm.handler.request.hand;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;

import com.hm.common.annotation.TokenValidate;
import com.hm.common.util.CommonDef;
import com.hm.handler.util.ResponseUtil;

/**
 * @author shishun.wang
 * @date 上午10:52:06 2016年5月31日
 * @version 1.0
 * @describe
 */
@Component
public class TokenValidateHandler {
	
	@Autowired
	private ResponseUtil responseUtil;

	public boolean handler(HttpServletRequest request, HttpServletResponse response, HandlerMethod method)
			throws Exception {
		TokenValidate tokenValidate = method.getMethodAnnotation(TokenValidate.class);
		if(null != tokenValidate){//验证token，在服务器中存在不
			String token = request.getParameter(CommonDef.BaseInit.SessionOrCookieToken.REQUEST_TOKEN);
			if(StringUtils.isEmpty(token)){
				return responseUtil.sendError(CommonDef.ResponseState.REQUEST_TOKEN_LOSE, response);
			}
		}
		return true;
	}
}
